<?php

/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/6/21
 * Time: 9:43
 */
namespace V1;
use Util\LogUtil;

class Fabulous extends \ResourceClass
{
    public $isActive = true;
    public $remark = '文章点赞';
    public function post(array $req, array $post, &$result) {
//        if (!array_key_exists('open_id',$req)) {
//            return 'open_id not null';
//        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['type'])) {
            return 'type not null';
        }
        switch ($data['type']){
            case 'fabulous':
                if (!array_key_exists('uid',$req)) {
                    return 'uid not null';
                }
                if(!isset($data['article_id'])) {
                    return '文章id 不能为空';
                }
               $fabuulous_lsit = new  \Orm\Fabulous\FabulousList();
               $fabuulous = new \Orm\Fabulous\FabulousClass();
               $result = array();
               try{
                   $fabuulous_lsit['where'] = 'uid = '.$req['uid'].' AND article_id ='.$data['article_id'];
                   $fabuulous_lsit ->load();
                   if (!$fabuulous_lsit['lists']) {
                       $fabuulous['open_id'] = '111';
                       $fabuulous['article_id'] = $data['article_id'];
                       $fabuulous['uid'] = $req['uid'];
                       $fabuulous['is_fabulous'] = $fabuulous::FABULOUS;
                       $fabuulous->insert();
                       $result = array('fabuulous_id'=>$fabuulous['id']);
                   }else{
                       $fabuulous ->loadById($fabuulous_lsit['lists'][0]['id']);
                       $fabuulous['is_fabulous'] = $fabuulous::FABULOUS;
                       $fabuulous ->update();
                       $result = array('fabuulous_id'=>$fabuulous['id']);
                   }
                }catch (\Exception $e){
                    throw new \Exception($e->getMessage());
                }
                break;
            case 'calloff':
                if (!isset($data['fabulous_id'])) {
                    return 'fabulous_id not null';
                }
                $fabuulous = new \Orm\Fabulous\FabulousClass();
                $fabuulous ->loadById($data['fabulous_id']);
                if ($fabuulous) {
                    $fabuulous['is_fabulous'] = $fabuulous::OFFFABULOUS;
                    $fabuulous->update();
                    $result = array('fabuulous_id'=>$fabuulous['id']);
                }
                break;
            default:
                return 'unkown options';
        }
    }

}